package com.qzsoft.zhjd.server.modules.asphalt.handler;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltAlarmEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltDataEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltDataSieveEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltMatchEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltMatchThresholdEntity;

/**
 * @ClassName AsphaltAlarmHandler
 * @Description 生产预警表处理
 * @author linhuawei
 * @since 2022-02-09
 */
public class AsphaltAlarmHandler {

    public static CollectAsphaltAlarmEntity coverTo(CollectAsphaltDataEntity data, CollectAsphaltDataSieveEntity sieveData,
                                                    CollectAsphaltMatchEntity match, CollectAsphaltMatchThresholdEntity threshold) {
        CollectAsphaltAlarmEntity entity = new CollectAsphaltAlarmEntity();
        entity.setSieve0d075(0);
        entity.setSieve0d15(0);
        entity.setSieve0d3(0);
        entity.setSieve0d6(0);
        entity.setSieve1d18(0);
        entity.setSieve2d36(0);
        entity.setSieve4d75(0);
        entity.setSieve9d5(0);
        entity.setSieve13d2(0);
        entity.setSieve16(0);
        entity.setSieve19(0);
        entity.setSieve26d5(0);
        entity.setSieve31d5(0);
        entity.setSieve37d5(0);
        entity.setSieve52(0);
        entity.setSieve53(0);
        entity.setSieve(0);// 筛孔预警
        entity.setTemperature(0);// 温度预警
        entity.setAsphaltAggregateRatio(0);// 油石比预警
        entity.setAlarm(0);// 整体预警

        StringBuffer details = new StringBuffer();
        BigDecimal max;
        BigDecimal min;
        BigDecimal value;
        Integer alarm;
        if (null != match) {
            // 判断油石比超标
            max = match.getAsphaltAggregateMax();
            min = match.getAsphaltAggregateMin();
            value = data.getAsphaltAggregateRatio();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                details.append("油石比超上限;");
            } else if (2 == alarm) {
                details.append("油石比超下限;");
            }
            entity.setAsphaltAggregateRatio(alarm);

            // 判断温度超标
            value = data.getDischargeTemperature();
            max = match.getTemperatureMax();
            min = match.getTemperatureMin();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                details.append("温度超上限;");
            } else if (2 == alarm) {
                details.append("温度超下限;");
            }
            entity.setTemperature(alarm);
        }

        // 判断筛孔通过率
        if (null != threshold) {
            // 超上限数据
            // 超下限数据
            List<String> upperSieve = new ArrayList<String>();
            List<String> lowerSieve = new ArrayList<String>();

            // 0.075筛孔
            value = sieveData.getSieve0d075();
            max = threshold.getSieve0d075max();
            min = threshold.getSieve0d075min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("0.075mm");
            } else if (2 == alarm) {
                lowerSieve.add("0.075mm");
            }
            entity.setSieve0d075(alarm);

            // 0.15筛孔
            value = sieveData.getSieve0d15();
            max = threshold.getSieve0d15max();
            min = threshold.getSieve0d15min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("0.15mm");
            } else if (2 == alarm) {
                lowerSieve.add("0.15mm");
            }
            entity.setSieve0d15(alarm);

            // 0.3筛孔
            value = sieveData.getSieve0d3();
            max = threshold.getSieve0d3max();
            min = threshold.getSieve0d3min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("0.3mm");
            } else if (2 == alarm) {
                lowerSieve.add("0.3mm");
            }
            entity.setSieve0d3(alarm);

            // 0.6筛孔
            value = sieveData.getSieve0d6();
            max = threshold.getSieve0d6max();
            min = threshold.getSieve0d6min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("0.6mm");
            } else if (2 == alarm) {
                lowerSieve.add("0.6mm");
            }
            entity.setSieve0d6(alarm);

            // 1.18筛孔
            value = sieveData.getSieve1d18();
            max = threshold.getSieve1d18max();
            min = threshold.getSieve1d18min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("1.18mm");
            } else if (2 == alarm) {
                lowerSieve.add("1.18mm");
            }
            entity.setSieve1d18(alarm);

            // 2.36筛孔
            value = sieveData.getSieve2d36();
            max = threshold.getSieve2d36max();
            min = threshold.getSieve2d36min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("2.36mm");
            } else if (2 == alarm) {
                lowerSieve.add("2.36mm");
            }
            entity.setSieve2d36(alarm);

            // 4.75筛孔
            value = sieveData.getSieve4d75();
            max = threshold.getSieve4d75max();
            min = threshold.getSieve4d75min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("4.75mm");
            } else if (2 == alarm) {
                lowerSieve.add("4.75mm");
            }
            entity.setSieve4d75(alarm);

            // 9.5筛孔
            value = sieveData.getSieve9d5();
            max = threshold.getSieve9d5max();
            min = threshold.getSieve9d5min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("9.5mm");
            } else if (2 == alarm) {
                lowerSieve.add("9.5mm");
            }
            entity.setSieve9d5(alarm);

            // 13.2筛孔
            value = sieveData.getSieve13d2();
            max = threshold.getSieve13d2max();
            min = threshold.getSieve13d2min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("13.2mm");
            } else if (2 == alarm) {
                lowerSieve.add("13.2mm");
            }
            entity.setSieve13d2(alarm);

            // 16筛孔
            value = sieveData.getSieve16();
            max = threshold.getSieve16max();
            min = threshold.getSieve16min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("16mm");
            } else if (2 == alarm) {
                lowerSieve.add("16mm");
            }
            entity.setSieve16(alarm);

            // 19筛孔
            value = sieveData.getSieve19();
            max = threshold.getSieve19max();
            min = threshold.getSieve19min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("19mm");
            } else if (2 == alarm) {
                lowerSieve.add("19mm");
            }
            entity.setSieve19(alarm);

            // 26.5筛孔
            value = sieveData.getSieve26d5();
            max = threshold.getSieve26d5max();
            min = threshold.getSieve26d5min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("26.5mm");
            } else if (2 == alarm) {
                lowerSieve.add("26.5mm");
            }
            entity.setSieve26d5(alarm);

            // 31.5筛孔
            value = sieveData.getSieve31d5();
            max = threshold.getSieve31d5max();
            min = threshold.getSieve31d5min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("31.5mm");
            } else if (2 == alarm) {
                lowerSieve.add("31.5mm");
            }
            entity.setSieve31d5(alarm);

            // 37.5筛孔
            value = sieveData.getSieve37d5();
            max = threshold.getSieve37d5max();
            min = threshold.getSieve37d5min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("37.5mm");
            } else if (2 == alarm) {
                lowerSieve.add("37.5mm");
            }
            entity.setSieve37d5(alarm);

            // 52筛孔
            value = sieveData.getSieve52();
            max = threshold.getSieve52max();
            min = threshold.getSieve52min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("52mm");
            } else if (2 == alarm) {
                lowerSieve.add("52mm");
            }
            entity.setSieve52(alarm);

            // 53筛孔
            value = sieveData.getSieve53();
            max = threshold.getSieve53max();
            min = threshold.getSieve53min();
            alarm = countAlarmLevel(value, max, min);
            if (1 == alarm) {
                upperSieve.add("53mm");
            } else if (2 == alarm) {
                lowerSieve.add("53mm");
            }
            entity.setSieve53(alarm);

            // 添加预警详情
            if (null != upperSieve && 0 < upperSieve.size()) {
                for (int i = 0, len = upperSieve.size(); i < len; i++) {
                    if (0 < i) {
                        details.append(",");
                    }
                    details.append(upperSieve.get(i));
                }
                details.append("上限超标;");
            }
            if (null != lowerSieve && 0 < lowerSieve.size()) {
                for (int i = 0, len = lowerSieve.size(); i < len; i++) {
                    if (0 < i) {
                        details.append(",");
                    }
                    details.append(lowerSieve.get(i));
                }
                details.append("下限超标;");
            }
        }

        // 判断筛孔预警
        alarm = entity.getSieve0d075() + entity.getSieve0d15() + entity.getSieve0d3() + entity.getSieve0d6()
                + entity.getSieve1d18() + entity.getSieve2d36() + entity.getSieve4d75() + entity.getSieve9d5()
                + entity.getSieve13d2() + entity.getSieve16() + entity.getSieve19() + entity.getSieve26d5()
                + entity.getSieve31d5() + entity.getSieve37d5() + entity.getSieve52() + entity.getSieve53();
        alarm = 0 == alarm ? 0 : 1;
        entity.setSieve(alarm);

        // 判断整体预警
        alarm = entity.getSieve() + entity.getTemperature() + entity.getAsphaltAggregateRatio();
        alarm = 0 == alarm ? 0 : 1;
        entity.setAlarm(alarm);

        // 有预警的数据处于未处理状态
        if (0 == alarm) {
            entity.setState(1);
        } else {
            entity.setState(0);
        }

        // 预警详情
        entity.setDetails(details.toString());
        return entity;
    }

    /**
     * @param value 当前值
     * @param max   最大值
     * @param min   最小值
     * @return 0正常，1超上限，2超下限
     * @Title countAlarmLevel
     * @Description 判断预警
     */
    private static int countAlarmLevel(BigDecimal value, BigDecimal max, BigDecimal min) {
        if (null != value && null != min && null != max) {
            if (0 == BigDecimal.ZERO.compareTo(min) && 0 == BigDecimal.ZERO.compareTo(max)) {
                return 0;
            }
            if (1 == value.compareTo(max)) {
                return 1;
            }
            if (-1 == value.compareTo(min)) {
                return 2;
            }
        }
        return 0;
    }

}
